package com.masterclient.app.businessmodule.vipmodule.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.bean.address.AddResultBean;
import com.eoner.baselibrary.bean.address.AddressListBean;
import com.eoner.baselibrary.bean.address.City;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.minemodule.address.AddressSelectDialog;
import com.masterclient.app.businessmodule.minemodule.address.contract.AddNewAddressContract;
import com.masterclient.app.businessmodule.minemodule.address.contract.AddNewAddressPresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加售后地址
 */
public class AddAddressActivity extends FCBusinessActivity<AddNewAddressPresenter> implements AddNewAddressContract.View {
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.street_edit)
    EditText streetEdit;
    @BindView(R.id.name_tv)
    EditText nameTv;
    @BindView(R.id.tel_tv)
    EditText telTv;
    private AddressSelectDialog dialog;
    private String mPositon = "3";
    String address1, address2, address3;
    private String as_id = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //窗口对齐屏幕宽度
        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        win.setAttributes(lp);
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
        fullScreen();
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {

    }

    private void openAreaDialog() {
        dialog = new AddressSelectDialog(this, "0");
        dialog.setOnSelectListener(new AddressSelectDialog.OnSelectListener() {
            @Override
            public void onSelect(String position, String id, String address) {
                mPositon = position;
                mPresenter.getRegionList(id);
                if ("4".equals(position)) {
                    address1 = address;
                } else if ("5".equals(position)) {
                    address2 = address;
                }
            }

            @Override
            public void onSelected(String id, String name) {
                dialog.dismiss();
                as_id = id;
                address3 = name;
                addressTv.setText(address1 + " " + address2 + " " + address3);

            }
        });

        dialog.show();
        mPresenter.getRegionList("0");
    }

    @Override
    public void getAddressList(AddressListBean.DataBean dataBean) {
        ArrayList<City> arrayList = new ArrayList<>();
        for (AddressListBean.DataBean.ShItemsBean bean : dataBean.getSh_items()) {
            City city = new City();
            city.setGrade(mPositon);
            city.setId(bean.getSh_id());
            city.setName(bean.getSh_name());
//            city.setIsMunicipality(bean.);
            arrayList.add(city);
        }
        dialog.setCities(arrayList);
    }

    @Override
    public void addAddressSuccess(AddResultBean.DataBean dataBean) {
        UserDataManager.getInstance().updateCheckRefundAddress(true);
        ToastUtil.showToast("提交成功");
        UserDataManager.getInstance().isShowAfterAddress = false;
        dialog.dismiss();
        finish();
    }


    @Override
    public void addAddressFailed(String msg) {
        ToastUtil.showToast("提交失败");
    }

    @Override
    public void onBackPressed() {
        setResult(50);
        finish();
    }

    @OnClick({R.id.close_iv, R.id.ok_tv, R.id.address_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_iv:
                setResult(50);
                finish();
                break;
            case R.id.ok_tv: {
                if (addressTv.getText().toString().length() <=0) {
                    ToastUtil.showToast("请选择所在地区");
                    return;
                }
                if (streetEdit.getText().toString().length() <=0) {
                    ToastUtil.showToast("请输入详细地址");
                    return;
                }
                if (nameTv.getText().toString().length() <=0) {
                    ToastUtil.showToast("请输入收件人姓名");
                    return;
                }
                if (telTv.getText().toString().length() <=0) {
                    ToastUtil.showToast("请输入联系人电话");
                    return;
                }
                String address = addressTv.getText().toString().trim();
                mPresenter.addNewAddress(as_id,address.replace(" ",""),"1",streetEdit.getText().toString(),telTv.getText().toString());

            }
                break;
            case R.id.address_tv:
                openAreaDialog();
                break;
        }
    }

    public void finish() {
        super.finish();
        //注释掉activity本身的过渡动画
        overridePendingTransition(0, R.anim.slide_out_from_bottom);
        UserDataManager.getInstance().isShowAfterAddress = false;
    }

    /**
     * 隐藏状态栏和底部虚拟按键
     */
    private void fullScreen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        }
    }
}
